{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   id  target  ps_ind_01  ps_ind_02_cat  ps_ind_03  ps_ind_04_cat  \\\n",
      "0   7       0          2              2          5              1   \n",
      "1   9       0          1              1          7              0   \n",
      "2  13       0          5              4          9              1   \n",
      "3  16       0          0              1          2              0   \n",
      "4  17       0          0              2          0              1   \n",
      "5  19       0          5              1          4              0   \n",
      "6  20       0          2              1          3              1   \n",
      "7  22       0          5              1          4              0   \n",
      "8  26       0          5              1          3              1   \n",
      "9  28       1          1              1          2              0   \n",
      "\n",
      "   ps_ind_05_cat  ps_ind_06_bin  ps_ind_07_bin  ps_ind_08_bin       ...        \\\n",
      "0              0              0              1              0       ...         \n",
      "1              0              0              0              1       ...         \n",
      "2              0              0              0              1       ...         \n",
      "3              0              1              0              0       ...         \n",
      "4              0              1              0              0       ...         \n",
      "5              0              0              0              0       ...         \n",
      "6              0              0              1              0       ...         \n",
      "7              0              1              0              0       ...         \n",
      "8              0              0              0              1       ...         \n",
      "9              0              0              1              0       ...         \n",
      "\n",
      "   ps_calc_11  ps_calc_12  ps_calc_13  ps_calc_14  ps_calc_15_bin  \\\n",
      "0           9           1           5           8               0   \n",
      "1           3           1           1           9               0   \n",
      "2           4           2           7           7               0   \n",
      "3           2           2           4           9               0   \n",
      "4           3           1           1           3               0   \n",
      "5           4           2           0           9               0   \n",
      "6           3           0           0          10               0   \n",
      "7           7           1           3           6               1   \n",
      "8           4           2           1           5               0   \n",
      "9           3           5           0           6               0   \n",
      "\n",
      "   ps_calc_16_bin  ps_calc_17_bin  ps_calc_18_bin  ps_calc_19_bin  \\\n",
      "0               1               1               0               0   \n",
      "1               1               1               0               1   \n",
      "2               1               1               0               1   \n",
      "3               0               0               0               0   \n",
      "4               0               0               1               1   \n",
      "5               1               0               1               1   \n",
      "6               1               0               0               1   \n",
      "7               0               1               0               1   \n",
      "8               1               0               0               0   \n",
      "9               1               0               0               1   \n",
      "\n",
      "   ps_calc_20_bin  \n",
      "0               1  \n",
      "1               0  \n",
      "2               0  \n",
      "3               0  \n",
      "4               0  \n",
      "5               1  \n",
      "6               0  \n",
      "7               0  \n",
      "8               1  \n",
      "9               0  \n",
      "\n",
      "[10 rows x 59 columns]\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "TRAIN_FILE = \"data/train.csv\"\n",
    "TEST_FILE = \"data/test.csv\"\n",
    "\n",
    "NUMERIC_COLS = [\n",
    "    \"ps_reg_01\", \"ps_reg_02\", \"ps_reg_03\",\n",
    "    \"ps_car_12\", \"ps_car_13\", \"ps_car_14\", \"ps_car_15\"\n",
    "]\n",
    "\n",
    "IGNORE_COLS = [\n",
    "    \"id\", \"target\",\n",
    "    \"ps_calc_01\", \"ps_calc_02\", \"ps_calc_03\", \"ps_calc_04\",\n",
    "    \"ps_calc_05\", \"ps_calc_06\", \"ps_calc_07\", \"ps_calc_08\",\n",
    "    \"ps_calc_09\", \"ps_calc_10\", \"ps_calc_11\", \"ps_calc_12\",\n",
    "    \"ps_calc_13\", \"ps_calc_14\",\n",
    "    \"ps_calc_15_bin\", \"ps_calc_16_bin\", \"ps_calc_17_bin\",\n",
    "    \"ps_calc_18_bin\", \"ps_calc_19_bin\", \"ps_calc_20_bin\"\n",
    "]\n",
    "\n",
    "dfTrain = pd.read_csv(TRAIN_FILE)\n",
    "dfTest = pd.read_csv(TEST_FILE)\n",
    "print(dfTrain.head(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>ps_calc_01</th>\n",
       "      <th>ps_calc_02</th>\n",
       "      <th>ps_calc_03</th>\n",
       "      <th>ps_calc_04</th>\n",
       "      <th>ps_calc_05</th>\n",
       "      <th>ps_calc_06</th>\n",
       "      <th>ps_calc_07</th>\n",
       "      <th>ps_calc_08</th>\n",
       "      <th>ps_calc_09</th>\n",
       "      <th>...</th>\n",
       "      <th>ps_ind_13_bin</th>\n",
       "      <th>ps_ind_14</th>\n",
       "      <th>ps_ind_15</th>\n",
       "      <th>ps_ind_16_bin</th>\n",
       "      <th>ps_ind_17_bin</th>\n",
       "      <th>ps_ind_18_bin</th>\n",
       "      <th>ps_reg_01</th>\n",
       "      <th>ps_reg_02</th>\n",
       "      <th>ps_reg_03</th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>7</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.7</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.718070</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>9</td>\n",
       "      <td>0.3</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.8</td>\n",
       "      <td>0.4</td>\n",
       "      <td>0.766078</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>13</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.7</td>\n",
       "      <td>0.1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>16</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0.9</td>\n",
       "      <td>0.1</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.9</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.580948</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>17</td>\n",
       "      <td>0.4</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>10</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.7</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0.840759</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>19</td>\n",
       "      <td>0.7</td>\n",
       "      <td>0.8</td>\n",
       "      <td>0.4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>2</td>\n",
       "      <td>11</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.9</td>\n",
       "      <td>1.8</td>\n",
       "      <td>2.332649</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>20</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0.5</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.617454</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>22</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>13</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.7</td>\n",
       "      <td>0.4</td>\n",
       "      <td>0.607248</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>26</td>\n",
       "      <td>0.9</td>\n",
       "      <td>0.8</td>\n",
       "      <td>0.6</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>3</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.9</td>\n",
       "      <td>0.7</td>\n",
       "      <td>0.901388</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>28</td>\n",
       "      <td>0.7</td>\n",
       "      <td>0.8</td>\n",
       "      <td>0.8</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>8</td>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.9</td>\n",
       "      <td>1.4</td>\n",
       "      <td>2.316652</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10 rows × 59 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   id  ps_calc_01  ps_calc_02  ps_calc_03  ps_calc_04  ps_calc_05  ps_calc_06  \\\n",
       "0   7         0.6         0.5         0.2           3           1          10   \n",
       "1   9         0.3         0.1         0.3           2           1           9   \n",
       "2  13         0.5         0.7         0.1           2           2           9   \n",
       "3  16         0.6         0.9         0.1           2           4           7   \n",
       "4  17         0.4         0.6         0.0           2           2           6   \n",
       "5  19         0.7         0.8         0.4           3           1           8   \n",
       "6  20         0.2         0.6         0.5           2           2           8   \n",
       "7  22         0.1         0.5         0.1           1           2           7   \n",
       "8  26         0.9         0.8         0.6           3           1           7   \n",
       "9  28         0.7         0.8         0.8           2           2           8   \n",
       "\n",
       "   ps_calc_07  ps_calc_08  ps_calc_09   ...    ps_ind_13_bin  ps_ind_14  \\\n",
       "0           1          10           1   ...                0          0   \n",
       "1           5           8           1   ...                0          0   \n",
       "2           1           8           2   ...                0          0   \n",
       "3           1           8           4   ...                0          0   \n",
       "4           3          10           2   ...                0          0   \n",
       "5           2          11           3   ...                0          0   \n",
       "6           1           8           3   ...                0          0   \n",
       "7           1           6           1   ...                0          0   \n",
       "8           3           9           4   ...                0          0   \n",
       "9           2           9           1   ...                0          0   \n",
       "\n",
       "   ps_ind_15  ps_ind_16_bin  ps_ind_17_bin  ps_ind_18_bin  ps_reg_01  \\\n",
       "0         11              0              1              0        0.7   \n",
       "1          3              0              0              1        0.8   \n",
       "2         12              1              0              0        0.0   \n",
       "3          8              1              0              0        0.9   \n",
       "4          9              1              0              0        0.7   \n",
       "5          6              1              0              0        0.9   \n",
       "6          8              1              0              0        0.6   \n",
       "7         13              1              0              0        0.7   \n",
       "8          6              1              0              0        0.9   \n",
       "9          4              0              0              1        0.9   \n",
       "\n",
       "   ps_reg_02  ps_reg_03  target  \n",
       "0        0.2   0.718070     0.0  \n",
       "1        0.4   0.766078     0.0  \n",
       "2        0.0  -1.000000     0.0  \n",
       "3        0.2   0.580948     0.0  \n",
       "4        0.6   0.840759     0.0  \n",
       "5        1.8   2.332649     0.0  \n",
       "6        0.1   0.617454     0.0  \n",
       "7        0.4   0.607248     0.0  \n",
       "8        0.7   0.901388     0.0  \n",
       "9        1.4   2.316652     1.0  \n",
       "\n",
       "[10 rows x 59 columns]"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.concat([dfTrain,dfTest])\n",
    "df.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "254\n",
      "{'ps_reg_01': 251, 'ps_car_13': 172, 'ps_car_04_cat': {0: 18, 1: 19, 2: 22, 3: 24, 4: 26, 5: 27, 6: 23, 7: 25, 8: 20, 9: 21}, 'ps_ind_16_bin': {0: 245, 1: 246}, 'ps_ind_12_bin': {0: 223, 1: 224}, 'ps_car_01_cat': {0: 9, 1: 11, 2: 10, 3: 8, 4: 6, 5: 5, 6: 3, 7: 2, 8: 7, 9: 4, 10: 0, 11: 1, -1: 12}, 'ps_reg_03': 253, 'ps_car_14': 173, 'ps_ind_06_bin': {0: 211, 1: 212}, 'ps_ind_01': {0: 178, 1: 176, 2: 175, 3: 180, 4: 179, 5: 177, 6: 181, 7: 182}, 'ps_ind_08_bin': {0: 215, 1: 216}, 'ps_car_03_cat': {0: 16, 1: 17, -1: 15}, 'ps_ind_02_cat': {1: 184, 2: 183, 3: 186, 4: 185, -1: 187}, 'ps_car_11': {0: 66, 1: 65, 2: 63, 3: 64}, 'ps_car_02_cat': {0: 14, 1: 13}, 'ps_car_05_cat': {0: 30, 1: 28, -1: 29}, 'ps_ind_03': {0: 192, 1: 195, 2: 191, 3: 194, 4: 193, 5: 188, 6: 197, 7: 189, 8: 198, 9: 190, 10: 199, 11: 196}, 'ps_ind_10_bin': {0: 219, 1: 220}, 'ps_car_09_cat': {0: 54, 1: 57, 2: 55, 3: 56, 4: 59, -1: 58}, 'ps_ind_07_bin': {0: 214, 1: 213}, 'ps_car_08_cat': {0: 52, 1: 53}, 'ps_ind_15': {0: 243, 1: 244, 2: 242, 3: 232, 4: 238, 5: 240, 6: 236, 7: 241, 8: 234, 9: 235, 10: 239, 11: 231, 12: 233, 13: 237}, 'ps_car_11_cat': {1: 99, 2: 122, 3: 107, 4: 162, 5: 86, 6: 153, 7: 105, 8: 106, 9: 164, 10: 90, 11: 166, 12: 67, 13: 142, 14: 168, 15: 143, 16: 112, 17: 165, 18: 136, 19: 68, 20: 75, 21: 137, 22: 100, 23: 121, 24: 85, 25: 110, 26: 91, 27: 109, 28: 87, 29: 83, 30: 73, 31: 103, 32: 93, 33: 132, 34: 104, 35: 155, 36: 76, 37: 139, 38: 94, 39: 116, 40: 114, 41: 79, 42: 118, 43: 81, 44: 126, 45: 167, 46: 108, 47: 170, 48: 140, 49: 97, 50: 161, 51: 148, 52: 150, 53: 145, 54: 92, 55: 159, 56: 157, 57: 154, 58: 163, 59: 80, 60: 69, 61: 111, 62: 131, 63: 151, 64: 82, 65: 146, 66: 89, 67: 133, 68: 74, 69: 113, 70: 141, 71: 123, 72: 128, 73: 134, 74: 138, 75: 119, 76: 115, 77: 135, 78: 102, 79: 149, 80: 125, 81: 169, 82: 71, 83: 95, 84: 160, 85: 101, 86: 130, 87: 88, 88: 117, 89: 96, 90: 124, 91: 120, 92: 127, 93: 98, 94: 152, 95: 84, 96: 129, 97: 158, 98: 156, 99: 72, 100: 147, 101: 77, 102: 144, 103: 78, 104: 70}, 'ps_car_06_cat': {0: 38, 1: 39, 2: 47, 3: 37, 4: 31, 5: 46, 6: 35, 7: 44, 8: 45, 9: 42, 10: 40, 11: 32, 12: 41, 13: 34, 14: 33, 15: 36, 16: 48, 17: 43}, 'ps_reg_02': 252, 'ps_ind_13_bin': {0: 225, 1: 226}, 'ps_car_07_cat': {0: 51, 1: 49, -1: 50}, 'ps_ind_18_bin': {0: 249, 1: 250}, 'ps_ind_05_cat': {0: 203, 1: 204, 2: 210, 3: 206, 4: 205, 5: 208, 6: 207, -1: 209}, 'ps_car_12': 171, 'ps_ind_11_bin': {0: 221, 1: 222}, 'ps_ind_17_bin': {0: 248, 1: 247}, 'ps_ind_04_cat': {0: 201, 1: 200, -1: 202}, 'ps_car_15': 174, 'ps_car_10_cat': {0: 61, 1: 60, 2: 62}, 'ps_ind_14': {0: 227, 1: 228, 2: 229, 3: 230}, 'ps_ind_09_bin': {0: 217, 1: 218}}\n"
     ]
    }
   ],
   "source": [
    "feature_dict = {}\n",
    "total_feature = 0\n",
    "for col in df.columns:\n",
    "    if col in IGNORE_COLS:\n",
    "        continue\n",
    "    elif col in NUMERIC_COLS:\n",
    "        feature_dict[col] = total_feature\n",
    "        total_feature += 1\n",
    "    else:\n",
    "        unique_val = df[col].unique()\n",
    "        feature_dict[col] = dict(zip(unique_val,range(total_feature,len(unique_val) + total_feature)))\n",
    "        total_feature += len(unique_val)\n",
    "print(total_feature)\n",
    "print(feature_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['id', 'target', 'ps_ind_01', 'ps_ind_02_cat', 'ps_ind_03',\n",
      "       'ps_ind_04_cat', 'ps_ind_05_cat', 'ps_ind_06_bin', 'ps_ind_07_bin',\n",
      "       'ps_ind_08_bin', 'ps_ind_09_bin', 'ps_ind_10_bin', 'ps_ind_11_bin',\n",
      "       'ps_ind_12_bin', 'ps_ind_13_bin', 'ps_ind_14', 'ps_ind_15',\n",
      "       'ps_ind_16_bin', 'ps_ind_17_bin', 'ps_ind_18_bin', 'ps_reg_01',\n",
      "       'ps_reg_02', 'ps_reg_03', 'ps_car_01_cat', 'ps_car_02_cat',\n",
      "       'ps_car_03_cat', 'ps_car_04_cat', 'ps_car_05_cat', 'ps_car_06_cat',\n",
      "       'ps_car_07_cat', 'ps_car_08_cat', 'ps_car_09_cat', 'ps_car_10_cat',\n",
      "       'ps_car_11_cat', 'ps_car_11', 'ps_car_12', 'ps_car_13', 'ps_car_14',\n",
      "       'ps_car_15', 'ps_calc_01', 'ps_calc_02', 'ps_calc_03', 'ps_calc_04',\n",
      "       'ps_calc_05', 'ps_calc_06', 'ps_calc_07', 'ps_calc_08', 'ps_calc_09',\n",
      "       'ps_calc_10', 'ps_calc_11', 'ps_calc_12', 'ps_calc_13', 'ps_calc_14',\n",
      "       'ps_calc_15_bin', 'ps_calc_16_bin', 'ps_calc_17_bin', 'ps_calc_18_bin',\n",
      "       'ps_calc_19_bin', 'ps_calc_20_bin'],\n",
      "      dtype='object')\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "对训练集进行转化\n",
    "\"\"\"\n",
    "print(dfTrain.columns)\n",
    "train_y = dfTrain[['target']].values.tolist()\n",
    "dfTrain.drop(['target','id'],axis=1,inplace=True)\n",
    "train_feature_index = dfTrain.copy()\n",
    "train_feature_value = dfTrain.copy()\n",
    "\n",
    "for col in train_feature_index.columns:\n",
    "    if col in IGNORE_COLS:\n",
    "        train_feature_index.drop(col,axis=1,inplace=True)\n",
    "        train_feature_value.drop(col,axis=1,inplace=True)\n",
    "        continue\n",
    "    elif col in NUMERIC_COLS:\n",
    "        train_feature_index[col] = feature_dict[col]\n",
    "    else:\n",
    "        train_feature_index[col] = train_feature_index[col].map(feature_dict[col])\n",
    "        train_feature_value[col] = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['id', 'ps_ind_01', 'ps_ind_02_cat', 'ps_ind_03', 'ps_ind_04_cat',\n",
      "       'ps_ind_05_cat', 'ps_ind_06_bin', 'ps_ind_07_bin', 'ps_ind_08_bin',\n",
      "       'ps_ind_09_bin', 'ps_ind_10_bin', 'ps_ind_11_bin', 'ps_ind_12_bin',\n",
      "       'ps_ind_13_bin', 'ps_ind_14', 'ps_ind_15', 'ps_ind_16_bin',\n",
      "       'ps_ind_17_bin', 'ps_ind_18_bin', 'ps_reg_01', 'ps_reg_02', 'ps_reg_03',\n",
      "       'ps_car_01_cat', 'ps_car_02_cat', 'ps_car_03_cat', 'ps_car_04_cat',\n",
      "       'ps_car_05_cat', 'ps_car_06_cat', 'ps_car_07_cat', 'ps_car_08_cat',\n",
      "       'ps_car_09_cat', 'ps_car_10_cat', 'ps_car_11_cat', 'ps_car_11',\n",
      "       'ps_car_12', 'ps_car_13', 'ps_car_14', 'ps_car_15', 'ps_calc_01',\n",
      "       'ps_calc_02', 'ps_calc_03', 'ps_calc_04', 'ps_calc_05', 'ps_calc_06',\n",
      "       'ps_calc_07', 'ps_calc_08', 'ps_calc_09', 'ps_calc_10', 'ps_calc_11',\n",
      "       'ps_calc_12', 'ps_calc_13', 'ps_calc_14', 'ps_calc_15_bin',\n",
      "       'ps_calc_16_bin', 'ps_calc_17_bin', 'ps_calc_18_bin', 'ps_calc_19_bin',\n",
      "       'ps_calc_20_bin'],\n",
      "      dtype='object')\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "对测试集进行转化\n",
    "\"\"\"\n",
    "print(dfTest.columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "对测试集进行转化\n",
    "\"\"\"\n",
    "test_ids = dfTest['id'].values.tolist()\n",
    "dfTest.drop(['id'],axis=1,inplace=True)\n",
    "\n",
    "test_feature_index = dfTest.copy()\n",
    "test_feature_value = dfTest.copy()\n",
    "\n",
    "for col in test_feature_index.columns:\n",
    "    if col in IGNORE_COLS:\n",
    "        test_feature_index.drop(col,axis=1,inplace=True)\n",
    "        test_feature_value.drop(col,axis=1,inplace=True)\n",
    "        continue\n",
    "    elif col in NUMERIC_COLS:\n",
    "        test_feature_index[col] = feature_dict[col]\n",
    "    else:\n",
    "        test_feature_index[col] = test_feature_index[col].map(feature_dict[col])\n",
    "        test_feature_value[col] = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   ps_ind_01  ps_ind_02_cat  ps_ind_03  ps_ind_04_cat  ps_ind_05_cat  \\\n",
      "0        175            183        188            200            203   \n",
      "1        176            184        189            201            203   \n",
      "2        177            185        190            200            203   \n",
      "\n",
      "   ps_ind_06_bin  ps_ind_07_bin  ps_ind_08_bin  ps_ind_09_bin  ps_ind_10_bin  \\\n",
      "0            211            213            215            217            219   \n",
      "1            211            214            216            217            219   \n",
      "2            211            214            216            217            219   \n",
      "\n",
      "     ...      ps_car_07_cat  ps_car_08_cat  ps_car_09_cat  ps_car_10_cat  \\\n",
      "0    ...                 49             52             54             60   \n",
      "1    ...                 49             53             55             60   \n",
      "2    ...                 49             53             55             60   \n",
      "\n",
      "   ps_car_11_cat  ps_car_11  ps_car_12  ps_car_13  ps_car_14  ps_car_15  \n",
      "0             67         63        171        172        173        174  \n",
      "1             68         64        171        172        173        174  \n",
      "2             69         65        171        172        173        174  \n",
      "\n",
      "[3 rows x 37 columns]\n",
      "   ps_ind_01  ps_ind_02_cat  ps_ind_03  ps_ind_04_cat  ps_ind_05_cat  \\\n",
      "0          1              1          1              1              1   \n",
      "1          1              1          1              1              1   \n",
      "2          1              1          1              1              1   \n",
      "\n",
      "   ps_ind_06_bin  ps_ind_07_bin  ps_ind_08_bin  ps_ind_09_bin  ps_ind_10_bin  \\\n",
      "0              1              1              1              1              1   \n",
      "1              1              1              1              1              1   \n",
      "2              1              1              1              1              1   \n",
      "\n",
      "     ...      ps_car_07_cat  ps_car_08_cat  ps_car_09_cat  ps_car_10_cat  \\\n",
      "0    ...                  1              1              1              1   \n",
      "1    ...                  1              1              1              1   \n",
      "2    ...                  1              1              1              1   \n",
      "\n",
      "   ps_car_11_cat  ps_car_11  ps_car_12  ps_car_13  ps_car_14  ps_car_15  \n",
      "0              1          1   0.400000   0.883679   0.370810   3.605551  \n",
      "1              1          1   0.316228   0.618817   0.388716   2.449490  \n",
      "2              1          1   0.316228   0.641586   0.347275   3.316625  \n",
      "\n",
      "[3 rows x 37 columns]\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "\"\"\"模型参数\"\"\"\n",
    "dfm_params = {\n",
    "    \"use_fm\":True,\n",
    "    \"use_deep\":True,\n",
    "    \"embedding_size\":8,\n",
    "    \"dropout_fm\":[1.0,1.0],\n",
    "    \"deep_layers\":[32,32],\n",
    "    \"dropout_deep\":[0.5,0.5,0.5],\n",
    "    \"deep_layer_activation\":tf.nn.relu,\n",
    "    \"epoch\":30,\n",
    "    \"batch_size\":1024,\n",
    "    \"learning_rate\":0.001,\n",
    "    \"optimizer\":\"adam\",\n",
    "    \"batch_norm\":1,\n",
    "    \"batch_norm_decay\":0.995,\n",
    "    \"l2_reg\":0.01,\n",
    "    \"verbose\":True,\n",
    "    \"eval_metric\":'gini_norm',\n",
    "    \"random_seed\":3\n",
    "}\n",
    "dfm_params['feature_size'] = total_feature\n",
    "dfm_params['field_size'] = len(train_feature_index.columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch 0,loss is 0.685435\n",
      "epoch 1,loss is 0.66566\n",
      "epoch 2,loss is 0.646201\n",
      "epoch 3,loss is 0.627191\n",
      "epoch 4,loss is 0.608893\n",
      "epoch 5,loss is 0.590952\n",
      "epoch 6,loss is 0.573173\n",
      "epoch 7,loss is 0.555565\n",
      "epoch 8,loss is 0.538144\n",
      "epoch 9,loss is 0.520927\n",
      "epoch 10,loss is 0.503927\n",
      "epoch 11,loss is 0.487159\n",
      "epoch 12,loss is 0.470661\n",
      "epoch 13,loss is 0.454482\n",
      "epoch 14,loss is 0.438646\n",
      "epoch 15,loss is 0.423134\n",
      "epoch 16,loss is 0.408061\n",
      "epoch 17,loss is 0.393481\n",
      "epoch 18,loss is 0.37923\n",
      "epoch 19,loss is 0.365215\n",
      "epoch 20,loss is 0.351495\n",
      "epoch 21,loss is 0.338165\n",
      "epoch 22,loss is 0.325289\n",
      "epoch 23,loss is 0.312871\n",
      "epoch 24,loss is 0.300872\n",
      "epoch 25,loss is 0.289289\n",
      "epoch 26,loss is 0.278148\n",
      "epoch 27,loss is 0.267508\n",
      "epoch 28,loss is 0.257415\n",
      "epoch 29,loss is 0.24787\n",
      "epoch 30,loss is 0.238866\n",
      "epoch 31,loss is 0.230405\n",
      "epoch 32,loss is 0.222434\n",
      "epoch 33,loss is 0.214963\n",
      "epoch 34,loss is 0.208039\n",
      "epoch 35,loss is 0.201682\n",
      "epoch 36,loss is 0.19589\n",
      "epoch 37,loss is 0.190655\n",
      "epoch 38,loss is 0.185966\n",
      "epoch 39,loss is 0.181807\n",
      "epoch 40,loss is 0.178156\n",
      "epoch 41,loss is 0.174989\n",
      "epoch 42,loss is 0.172274\n",
      "epoch 43,loss is 0.169981\n",
      "epoch 44,loss is 0.168076\n",
      "epoch 45,loss is 0.166525\n",
      "epoch 46,loss is 0.16529\n",
      "epoch 47,loss is 0.164338\n",
      "epoch 48,loss is 0.16363\n",
      "epoch 49,loss is 0.163133\n",
      "epoch 50,loss is 0.162811\n",
      "epoch 51,loss is 0.162633\n",
      "epoch 52,loss is 0.162569\n",
      "epoch 53,loss is 0.162588\n",
      "epoch 54,loss is 0.162668\n",
      "epoch 55,loss is 0.162786\n",
      "epoch 56,loss is 0.162924\n",
      "epoch 57,loss is 0.163067\n",
      "epoch 58,loss is 0.163201\n",
      "epoch 59,loss is 0.163318\n",
      "epoch 60,loss is 0.16341\n",
      "epoch 61,loss is 0.163473\n",
      "epoch 62,loss is 0.163505\n",
      "epoch 63,loss is 0.163505\n",
      "epoch 64,loss is 0.163475\n",
      "epoch 65,loss is 0.163416\n",
      "epoch 66,loss is 0.163331\n",
      "epoch 67,loss is 0.163225\n",
      "epoch 68,loss is 0.163101\n",
      "epoch 69,loss is 0.162963\n",
      "epoch 70,loss is 0.162816\n",
      "epoch 71,loss is 0.162662\n",
      "epoch 72,loss is 0.162507\n",
      "epoch 73,loss is 0.162351\n",
      "epoch 74,loss is 0.162199\n",
      "epoch 75,loss is 0.162052\n",
      "epoch 76,loss is 0.161911\n",
      "epoch 77,loss is 0.161779\n",
      "epoch 78,loss is 0.161655\n",
      "epoch 79,loss is 0.161541\n",
      "epoch 80,loss is 0.161436\n",
      "epoch 81,loss is 0.161339\n",
      "epoch 82,loss is 0.161252\n",
      "epoch 83,loss is 0.161172\n",
      "epoch 84,loss is 0.161099\n",
      "epoch 85,loss is 0.161032\n",
      "epoch 86,loss is 0.160971\n",
      "epoch 87,loss is 0.160914\n",
      "epoch 88,loss is 0.160861\n",
      "epoch 89,loss is 0.16081\n",
      "epoch 90,loss is 0.160761\n",
      "epoch 91,loss is 0.160713\n",
      "epoch 92,loss is 0.160667\n",
      "epoch 93,loss is 0.16062\n",
      "epoch 94,loss is 0.160573\n",
      "epoch 95,loss is 0.160525\n",
      "epoch 96,loss is 0.160477\n",
      "epoch 97,loss is 0.160427\n",
      "epoch 98,loss is 0.160377\n",
      "epoch 99,loss is 0.160325\n"
     ]
    }
   ],
   "source": [
    "\"\"\"开始建立模型\"\"\"\n",
    "feat_index = tf.placeholder(tf.int32,shape=[None,None],name='feat_index')\n",
    "feat_value = tf.placeholder(tf.float32,shape=[None,None],name='feat_value')\n",
    "\n",
    "label = tf.placeholder(tf.float32,shape=[None],name='label')\n",
    "\n",
    "\"\"\"建立weights\"\"\"\n",
    "weights = dict()\n",
    "\n",
    "#embeddings\n",
    "weights['feature_embeddings'] = tf.Variable(\n",
    "    tf.random_normal([dfm_params['feature_size'],dfm_params['embedding_size']],0.0,0.01),\n",
    "    name='feature_embeddings')\n",
    "weights['feature_bias'] = tf.Variable(tf.random_normal([dfm_params['feature_size'],1],0.0,1.0),name='feature_bias')\n",
    "\n",
    "\n",
    "#deep layers\n",
    "num_layer = len(dfm_params['deep_layers'])\n",
    "input_size = dfm_params['field_size'] * dfm_params['embedding_size']\n",
    "glorot = np.sqrt(2.0/(input_size + dfm_params['deep_layers'][0]))\n",
    "\n",
    "weights['layer_0'] = tf.Variable(\n",
    "    np.random.normal(loc=0,scale=glorot,size=(input_size,dfm_params['deep_layers'][0])),dtype=np.float32\n",
    ")\n",
    "weights['bias_0'] = tf.Variable(\n",
    "    np.random.normal(loc=0,scale=glorot,size=(1,dfm_params['deep_layers'][0])),dtype=np.float32\n",
    ")\n",
    "\n",
    "\n",
    "for i in range(1,num_layer):\n",
    "    glorot = np.sqrt(2.0 / (dfm_params['deep_layers'][i - 1] + dfm_params['deep_layers'][i]))\n",
    "    weights[\"layer_%d\" % i] = tf.Variable(\n",
    "        np.random.normal(loc=0, scale=glorot, size=(dfm_params['deep_layers'][i - 1], dfm_params['deep_layers'][i])),\n",
    "        dtype=np.float32)  # layers[i-1] * layers[i]\n",
    "    weights[\"bias_%d\" % i] = tf.Variable(\n",
    "        np.random.normal(loc=0, scale=glorot, size=(1, dfm_params['deep_layers'][i])),\n",
    "        dtype=np.float32)  # 1 * layer[i]\n",
    "\n",
    "\n",
    "# final concat projection layer\n",
    "\n",
    "if dfm_params['use_fm'] and dfm_params['use_deep']:\n",
    "    input_size = dfm_params['field_size'] + dfm_params['embedding_size'] + dfm_params['deep_layers'][-1]\n",
    "elif dfm_params['use_fm']:\n",
    "    input_size = dfm_params['field_size'] + dfm_params['embedding_size']\n",
    "elif dfm_params['use_deep']:\n",
    "    input_size = dfm_params['deep_layers'][-1]\n",
    "\n",
    "glorot = np.sqrt(2.0/(input_size + 1))\n",
    "weights['concat_projection'] = tf.Variable(np.random.normal(loc=0,scale=glorot,size=(input_size,1)),dtype=np.float32)\n",
    "weights['concat_bias'] = tf.Variable(tf.constant(0.01),dtype=np.float32)\n",
    "\n",
    "\"\"\"embedding\"\"\"\n",
    "embeddings = tf.nn.embedding_lookup(weights['feature_embeddings'],feat_index)\n",
    "\n",
    "reshaped_feat_value = tf.reshape(feat_value,shape=[-1,dfm_params['field_size'],1])\n",
    "\n",
    "embeddings = tf.multiply(embeddings,reshaped_feat_value)\n",
    "\n",
    "\n",
    "\"\"\"fm part\"\"\"\n",
    "fm_first_order = tf.nn.embedding_lookup(weights['feature_embeddings'],feat_index)\n",
    "fm_first_order = tf.reduce_sum(tf.multiply(fm_first_order,reshaped_feat_value),2)\n",
    "\n",
    "summed_features_emb = tf.reduce_sum(embeddings,1)\n",
    "summed_features_emb_square = tf.square(summed_features_emb)\n",
    "\n",
    "squared_features_emb = tf.square(embeddings)\n",
    "squared_sum_features_emb = tf.reduce_sum(squared_features_emb,1)\n",
    "\n",
    "fm_second_order = 0.5 * tf.subtract(summed_features_emb_square,squared_sum_features_emb)\n",
    "\n",
    "\"\"\"deep part\"\"\"\n",
    "y_deep = tf.reshape(embeddings,shape=[-1,dfm_params['field_size'] * dfm_params['embedding_size']])\n",
    "\n",
    "for i in range(0,len(dfm_params['deep_layers'])):\n",
    "    y_deep = tf.add(tf.matmul(y_deep,weights[\"layer_%d\" %i]), weights[\"bias_%d\"%i])\n",
    "    y_deep = tf.nn.relu(y_deep)\n",
    "    \n",
    "\"\"\"final layer\"\"\"\n",
    "if dfm_params['use_fm'] and dfm_params['use_deep']:\n",
    "    concat_input = tf.concat([fm_first_order,fm_second_order,y_deep],axis=1)\n",
    "elif dfm_params['use_fm']:\n",
    "    concat_input = tf.concat([fm_first_order,fm_second_order],axis=1)\n",
    "elif dfm_params['use_deep']:\n",
    "    concat_input = y_deep\n",
    "    \n",
    "out = tf.nn.sigmoid(tf.add(tf.matmul(concat_input,weights['concat_projection']),weights['concat_bias']))\n",
    "\n",
    "\"\"\"loss and optimizer\"\"\"\n",
    "loss = tf.losses.log_loss(tf.reshape(label,(-1,1)), out)\n",
    "optimizer = tf.train.AdamOptimizer(learning_rate=dfm_params['learning_rate'], beta1=0.9, beta2=0.999,\n",
    "                                                        epsilon=1e-8).minimize(loss)\n",
    "\n",
    "\"\"\"train\"\"\"\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    for i in range(100):\n",
    "        epoch_loss,_ = sess.run([loss,optimizer],feed_dict={feat_index:train_feature_index,\n",
    "                             feat_value:train_feature_value,\n",
    "                             label:train_y})\n",
    "        print(\"epoch %s,loss is %s\" % (str(i),str(epoch_loss)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python35",
   "language": "python",
   "name": "python35"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
